<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $Addvermodel=D('Addver');

        $addverlist = $Addvermodel->getAddver();
        foreach ($addverlist as &$value) {
           $value['name']=$this->str($value['name']);
        }
        $goodsmodel=D('Goods'); 

        $hotlist = $goodsmodel->getHotgoods();
        // var_dump($hotlist); exit;
        foreach ($hotlist as &$val) {
            $val['giname']=$this->str($val['giname']);
        }
        $newlist = $goodsmodel->getNewgoods();
        foreach ($newlist as &$value) {
            $value['giname']=$this->str($value['giname']);
        }
        // var_dump($hotlist);exit;
        $this->assign('login',$_SESSION['home']);
        $this->assign('hotlist',$hotlist);
        $this->assign('newlist',$newlist);
        $this->assign('addverlist',$addverlist);

        $this->display();
    }
    public function more(){
        $goodsmodel=D('Goods'); 
        if($_GET['hot']){
            $hot=$_GET['hot'];
            $hotlist = $goodsmodel->getHotgoods(1);
        // var_dump($hotlist); exit;
            foreach ($hotlist as &$val) {
            $val['giname']=$this->str($val['giname']);
            }
            $this->assign('login',$_SESSION['home']);
            $this->assign('list',$hotlist);
            $name='热销商品';
            $this->assign('name',$name);


        }else{
            $new=$_GET['new'];
            $newlist = $goodsmodel->getNewgoods(1);
            foreach ($newlist as &$value) {
                $value['giname']=$this->str($value['giname']);
            }
            $this->assign('login',$_SESSION['home']);
            $this->assign('list',$newlist);
            $name='新品';
            $this->assign('name',$name);

        }
        $Addvermodel=D('Addver');
        $addverlist = $Addvermodel->getAddver();
        foreach ($addverlist as &$value) {
           $value['name']=$this->str($value['name']);
        }
        $this->assign('addverlist',$addverlist);
        $this->display();
    }
    public function detail(){
        if(empty($_GET)){
            $this->redirect('Index/index');
            return false;
        }
        $id=$_GET['id'];
        $goodsmodel=D('Goods'); 
        $hotlist = $goodsmodel->getHotgoods();
        foreach ($hotlist as &$val) {
            $val['giname']=$this->str($val['giname']);
        }
        $newlist = $goodsmodel->getNewgoods();
        foreach ($newlist as &$value) {
            $value['giname']=$this->str($value['giname']);
        }
        $list = $goodsmodel->getGoods($id);
        // var_dump($list);exit;
        $this->assign('login',$_SESSION['home']);
        $list['giname']=$this->str($list['giname']);      
        $this->assign('list',$list);
        $this->assign('hotlist',$hotlist);
        $this->assign('newlist',$newlist);
        $this->display();


    }

    public function str($str,$zoom=''){
        $str='/Public/Uploads/'.substr($str,0,4).'/'.substr($str,4,2).'/'.substr($str,6,2).'/'.$zoom.$str;
        return $str;
    }
    public function login(){
    	if(empty($_POST)){
    		$this->display();
    		return false;
    	}
    	$res=$this->check($_POST['yzm']);
    	if(!$res){
    		$this->error('验证码错误');
    	}

    	$userModel=D('user');
    	$data=$userModel->create();
    	if(!$data){
    		$this->error($userModel->getError());
    	}else{
    		unset($data['regtime']);
    		$res=$userModel->getFind($data);

    		if(is_null($res)){
    			$this->error('登录有问题请重新登录');
    		}else{
                unset($res['pwd']);
    			$_SESSION['home']=$res;
    			$this->success('登录成功',U('Index'),2);
    		}
    	}
    }

    public function loginout(){

    	unset($_SESSION['home']);
        unset($_SESSION['cart']);
    	$this->success('退出成功',U('Index'),2);
  }


    public function reg(){
        if(empty($_POST['name'])||empty($_POST['pwd'])||empty($_POST['rpwd'])){
        	$this->display('Index/reg');
        	return false;
        }

        $UserModel=D('user');
        $da=$UserModel->select();
        foreach ($da as $key => $value) {
            if($_POST['name']==$value['name']){
               $this->display('Index/reg');
                return false; 
            }   
        }
        $data = $UserModel->create();
        $res=$UserModel->getAdd($data);
        if($res){
        	$this->success('注册成功',U('login'),2);
        }else{
        	$this->error('注册失败');
        }
    }


    public function yzm(){

    	$Verify = new  \Think\Verify();
    	$Verify->fontSize =50;
    	$Verify->length  =2   ;
    	$Verify->useNoise = false;
    	$Verify->entry();
    }

    private function check($code,$id){
    	$verify = new \Think\Verify();
    	return $verify->check($code,$id);
    }
}